package com.yinp.proapp.module.work.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewbinding.ViewBinding;

import com.yinp.proapp.R;
import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.bean.BaseBean2;
import com.yinp.proapp.databinding.ActivityTestSelectBinding;
import com.yinp.proapp.databinding.DialogSelectStyleOneBinding;
import com.yinp.proapp.databinding.DialogSelectStyleTwoBinding;
import com.yinp.proapp.databinding.ItemDialogSelectTwoBinding;
import com.yinp.proapp.utils.StatusBarUtil;
import com.yinp.tools.fragment_dialog.BaseDialogFragment;
import com.yinp.tools.fragment_dialog.CommonDialogFragment;
import com.yinp.tools.fragment_dialog.DialogFragmentHolder;
import com.yinp.tools.fragment_dialog.ViewConvertListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yinp
 * @Date: 2021/8/30
 * @Description:一些通用的选择方式古
 */
public class TestSelectActivity extends AppBaseActivity<ActivityTestSelectBinding> {
    private CommonAdapter<BaseBean2> twoAdapter;
    private List<BaseBean2> twoList = new ArrayList<>();

    private List<BaseBean2> twoSingleList = new ArrayList<>();
    private List<BaseBean2> twoMoreList = new ArrayList<>();
    private int twoPosition = 3;

    private boolean twoSingleIs;

    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        initClick(bd.btnOne, bd.btnTwo, bd.header.headerBackImg, bd.btnThree);
        initRecycler();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == bd.header.headerBackImg) {
            finish();
        } else if (v == bd.btnOne) {
            selectStyleOneDialog();
        } else if (v == bd.btnTwo) {
            twoSingleIs = true;
            twoList.clear();
            for (int i = 0; i < 20; i++) {
                twoSingleList.add(new BaseBean2(String.valueOf(i)));
            }
            twoList.addAll(twoSingleList);
            selectStyleTwoDialog("单选");
        } else if (v == bd.btnThree) {
            twoSingleIs = false;
            twoList.clear();
            for (int i = 0; i < 20; i++) {
                twoMoreList.add(new BaseBean2(String.valueOf(i)));
            }
            twoList.addAll(twoMoreList);
            selectStyleTwoDialog("多选");
        }
    }

    private void initRecycler() {
        twoAdapter = new CommonAdapter<BaseBean2>(mContext, twoList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                return new SingleViewHolder(ItemDialogSelectTwoBinding.inflate(mInflater, parent, false));
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, BaseBean2 item) {
                ItemDialogSelectTwoBinding binding = (ItemDialogSelectTwoBinding) holder.getBinding();
                binding.tvContent.setText(item.getTitle());
                if (twoSingleIs) {
                    if (twoPosition == position) {
                        binding.tvContent.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                        binding.sllContent.setColor(ContextCompat.getColor(mContext, R.color.ff4d4d));
                    } else {
                        binding.tvContent.setTextColor(ContextCompat.getColor(mContext, R.color._333));
                        binding.sllContent.setColor(ContextCompat.getColor(mContext, R.color.white));
                    }
                } else {
                    if (item.isSelected()) {
                        binding.tvContent.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                        binding.sllContent.setColor(ContextCompat.getColor(mContext, R.color.ff4d4d));
                    } else {
                        binding.tvContent.setTextColor(ContextCompat.getColor(mContext, R.color._333));
                        binding.sllContent.setColor(ContextCompat.getColor(mContext, R.color.white));
                    }
                }
            }
        };
        twoAdapter.setOnItemClickListener((position, view) -> {
            BaseBean2 bean2 = twoList.get(position);
            if (twoSingleIs) {
                twoPosition = position;
                bean2.setPosition(position);
                bd.btnTwo.setText(bean2.getTitle());
                twoAdapter.notifyItemChanged(position, "");
                if (twoDialogFragment != null) {
                    twoDialogFragment.dismiss();
                }
            } else {
                bd.btnThree.setText("多选");
                twoList.get(position).setSelected(!twoList.get(position).isSelected());
                twoAdapter.notifyItemChanged(position);
            }
        });
    }

    private List<String> oneList = new ArrayList<>();

    /**
     * 弹窗滑动选择
     */
    private void selectStyleOneDialog() {
        if (oneList.size() == 0) {
            for (int i = 0; i < 10; i++) {
                oneList.add(String.valueOf(i));
            }
        }
        CommonDialogFragment.newInstance().setLayout(DialogSelectStyleOneBinding.inflate(LayoutInflater.from(mContext), null, false))
                .setViewConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(DialogFragmentHolder holder, BaseDialogFragment dialogFragment, ViewBinding viewBinding) {
                        DialogSelectStyleOneBinding binding = (DialogSelectStyleOneBinding) viewBinding;
                        binding.tvLeft.setOnClickListener(v -> dialogFragment.dismiss());
                        binding.tvRight.setOnClickListener(v -> {
                            dialogFragment.dismiss();
                            bd.btnOne.setText(oneList.get(binding.csvSelect.getSelected()));
                        });
                        binding.csvSelect.setDataList(oneList);
                        binding.csvSelect.setSelected(3);
                    }
                }).setAnimStyle(R.style.BottomDialogAnimation).setGravity(BaseDialogFragment.BOTTOM).setPercentSize(1, 0.5f).show(getSupportFragmentManager());
    }

    private BaseDialogFragment twoDialogFragment;

    private void selectStyleTwoDialog(String title) {
        CommonDialogFragment.newInstance().setLayout(DialogSelectStyleTwoBinding.inflate(LayoutInflater.from(mContext), null, false))
                .setViewConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(DialogFragmentHolder holder, BaseDialogFragment dialogFragment, ViewBinding viewBinding) {
                        DialogSelectStyleTwoBinding binding = (DialogSelectStyleTwoBinding) viewBinding;
                        twoDialogFragment = dialogFragment;
                        binding.tvTitle.setText(title);
                        if (twoSingleIs) {
                            binding.btnSure.setVisibility(View.GONE);
                        } else {
                            binding.btnSure.setVisibility(View.VISIBLE);
                            binding.btnSure.setOnClickListener(v -> {
                                StringBuilder stringBuilder = new StringBuilder();
                                for (int i = 0; i < twoList.size(); i++) {
                                    if (twoList.get(i).isSelected()) {
                                        stringBuilder.append(twoList.get(i).getTitle());
                                        stringBuilder.append(",");
                                    }
                                }
                                bd.tvMoreSelect.setText(stringBuilder.substring(0, stringBuilder.length() - 1));
                                dialogFragment.dismiss();
                            });
                        }
                        if (twoAdapter != null && binding.rvList.getAdapter() == null) {
                            binding.rvList.setLayoutManager(new LinearLayoutManager(mContext));
                            binding.rvList.setHasFixedSize(true);
                            binding.rvList.setAdapter(twoAdapter);
                        }
                    }
                }).setAnimStyle(R.style.BottomDialogAnimation).setGravity(BaseDialogFragment.BOTTOM).setPercentSize(1, 0.5f).show(getSupportFragmentManager());
    }
}